package com.hutianao.gqgl.department.service.impl;

import com.hutianao.gqgl.department.entity.SearchDepartmentEntity;
import com.hutianao.gqgl.department.repository.DepartmentDao;
import com.hutianao.gqgl.department.service.DepartmentService;
import com.hutianao.gqgl.job.entity.SearchJobEntity;
import com.hutianao.gqgl.mybatis.entity.PtDepartmentEntity;
import com.hutianao.gqgl.mybatis.entity.PtJobEntity;
import com.hutianao.gqgl.mybatis.entity.PtUserEntity;
import com.hutianao.gqgl.mybatis.flexigrid.PaddingEntity;
import com.hutianao.gqgl.mybatis.flexigrid.PadingRstType;
import com.hutianao.gqgl.mybatis.ztree.ZtreeEntity;
import com.hutianao.gqgl.spring.control.BaseController;
import com.hutianao.gqgl.spring.util.DataUitl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;

@Service
public class DepartmentServiceImpl extends BaseController implements DepartmentService {
    @Autowired
    private DepartmentDao departDao;

    @Value("${unbound.id}")
    private String unboundId;

    @Override
    public List<ZtreeEntity> queryLeftTree() {
        List<ZtreeEntity> list = departDao.queryLeftTree();
        list.add(getNoBoundNode());
        return list;
    }
    @Override
    public boolean deleteDepartInfoByIds(String depUuid) {
        int department = departDao.querySubdep(depUuid);
        int user = departDao.querySubuser(depUuid);
        if(department >0){
            return false;
        }
        if(user >0 ){
            return false;
        }
        departDao.deleteDepartInfoByIds(depUuid);
        return true;
    }




    @Override
    public void unboundParentIdByDepUuid(String depUuid) {
        departDao.unboundParentIdByDepUuid(depUuid,unboundId);
    }



    @Override
    public PtDepartmentEntity queryDepartListById(String depUuid) {
        return departDao.queryDepartListById(depUuid);
    }
    @Override
    public List<ZtreeEntity> queryBoundDepartTree(String depUuid) {
        List<ZtreeEntity> list = departDao.queryBoundDepartTree();
        String parentId = departDao.queryParentId(depUuid);
        for(ZtreeEntity item:list){
            if(parentId.equals(item.getId())){
                item.setChecked(true);
                break;
            }
        }
        return list;
    }



    @Override
    public void saveParentIdByDepUuid(String depUuid, String parentId) {
        departDao.saveParentIdByDepUuid(depUuid,parentId);
    }

    @Override
    public List<ZtreeEntity> queryDepartTree(String depUuid) {
        List<ZtreeEntity> list= departDao.queryDepartTree(depUuid);
        String parentId = departDao.queryParentId(depUuid);
        for(ZtreeEntity item:list){
            if(parentId.equals(item.getId())){
                item.setChecked(true);
                break;
            }
        }
        return list;
    }

    @Override
    public void saveBelongCenterIdByDepUuid(String depUuid, String belongCenter) {
        departDao.saveBelongCenterIdByDepUuid(depUuid,belongCenter);
    }

    @Override
    public PadingRstType<PtDepartmentEntity> queryDepartInfoListById(String depUuid, PaddingEntity padding) {
        return null;
    }

    @Override
    public PadingRstType<PtDepartmentEntity> queryDepartInfoListByPage(SearchJobEntity search, PaddingEntity padding) {
        padding.deal(PtDepartmentEntity.class);
        PadingRstType<PtDepartmentEntity> padingRstType = new PadingRstType<>();
        padingRstType.setPage(padding.getPage());
        List<PtDepartmentEntity> list = departDao.queryDepartInfoListByPage(search,padding);
        padingRstType.setRawRecords(list);
        Integer total = departDao.queryDepartInfoListTotal(search);
        padingRstType.setTotal(total);
        padingRstType.putItems();

        return padingRstType;
    }
    @Override
    public void addDepartInfo(PtDepartmentEntity ptDepartEntity) {
        ptDepartEntity.setDepUuid(DataUitl.getUuid());
        departDao.insertDepartInfo(ptDepartEntity);
    }

    @Override
    public void updateDepartById(PtDepartmentEntity ptDepartEntity) {
        departDao.updateDepartById(ptDepartEntity);
    }

    @Override
    public PadingRstType<PtUserEntity> queryUserListByPage(SearchDepartmentEntity search, PaddingEntity padding) {
        padding.deal(PtUserEntity.class);
        PadingRstType<PtUserEntity> padingRstType = new PadingRstType<>();
        padingRstType.setPage(padding.getPage());
        List<PtUserEntity> list = departDao.queryUserInfoListByPage(search,padding);
        padingRstType.setRawRecords(list);
        padingRstType.putItems();

        return padingRstType;
    }

    @Override
    public PadingRstType<PtUserEntity> queryUserInfoListByPage(SearchDepartmentEntity search, PaddingEntity padding) {
        padding.deal(PtUserEntity.class);
        PadingRstType<PtUserEntity> padingRstType = new PadingRstType<>();
        padingRstType.setPage(padding.getPage());
        List<PtUserEntity> list = departDao.queryUserInfoListByPage(search,padding);
        padingRstType.setRawRecords(list);
        padingRstType.putItems();

        return padingRstType;
    }
    @Override
    public PadingRstType<PtJobEntity> queryDutyListByPage(SearchDepartmentEntity search, PaddingEntity padding) {
        padding.deal(PtJobEntity.class);
        PadingRstType<PtJobEntity> padingRstType = new PadingRstType<>();
        padingRstType.setPage(padding.getPage());
        List<PtJobEntity> list = departDao.queryDutyListByPage(search,padding);
        padingRstType.setRawRecords(list);
        padingRstType.putItems();

        return padingRstType;
    }
}
